package com.mylike.api.dev;

import com.alibaba.fastjson.JSONObject;
import com.mylike.socket.socket.WebSocketServer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import utils.CommonUtill;
import utils.HttpClientUtil;
import utils.JsonStringConstant;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;
import java.util.Map.Entry;

@Controller
@RequestMapping("/api/dev")
public class DevManager {

    private CommonUtill commonUtill = new CommonUtill();

    @RequestMapping("/devmgr")
    public ModelAndView devmgr(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mode = new ModelAndView();

        String contextPath = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName();
        if (request.getServerPort() != 80) {
            basePath += ":" + request.getServerPort();
        }
        basePath += contextPath;
        mode.addObject("basePath", basePath);

        mode.setViewName("dev-mgr/dev-main");
        return mode;
    }

    @RequestMapping(value = "/saveDevInfo")
    public void saveDevInfo(@RequestBody String param, HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject jsonObject = new JSONObject();
        Map<String, Object> paramMap = (Map<String, Object>) jsonObject.parse(param);
        System.out.println(paramMap.toString());

        String contextPath = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName();
        if (request.getServerPort() != 80) {
            basePath += ":" + request.getServerPort();
        }
        basePath += contextPath;
        String url = basePath + "/socketmsg/devMsg";

        Map<String, String> paramMsg = new HashMap<String, String>();
        paramMsg.put("msg", "3333");

        //HttpClientUtil.postRequest(url,paramMsg);
        WebSocketServer.sendInfo("你扫我干嘛？" + System.currentTimeMillis(), "dev-main");
    }

    @RequestMapping(value = "/saveImgs")
    public void saveImgs(HttpServletResponse response, HttpServletRequest request) throws Exception {
        JSONObject jsonString = null;
        String aaa = request.getParameter("userId");
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            if (multipartRequest != null && !multipartRequest.equals("")) {

                List<MultipartFile> fileMap1 = multipartRequest.getFiles("abc");
                Iterator<MultipartFile> it = fileMap1.iterator();

                //--------------------------------------------------
                // 获得图片：
                /*Map fileMap = multipartRequest.getFileMap();
                Set set = fileMap.entrySet();
                Iterator it = set.iterator();*/
                //---------------------------------------------------------------------

                while (it.hasNext()) {
//                    Entry entry = (Entry) it.next();
//                    String key = (String) entry.getKey();

                    int nmOrder = 0;
//                    if(null != key && !"".equals(key)){
//                        String  stOrder = key.substring(key.length()-1, key.length());
//                        nmOrder = Integer.valueOf(stOrder);
//                    }


                    MultipartFile file = (MultipartFile) it.next();


//                    MultipartFile file = (MultipartFile) entry.getValue();
                    if (null != file) {
                        String fileName = file.getOriginalFilename();
                        //String fileid = UUID.randomUUID().toString();
                        //String prefix = fileName.substring(fileName.lastIndexOf("."), fileName.length());

                        //tMember.setStHeadLogo(tMember.getStMemberId()+prefix);
                        File targetFile = new File("d://wechatImgs/" + fileName);

//                        if(!targetFile.exists()){
//                            targetFile.mkdirs();
//                        }
                        //保存
                        try {
                            file.transferTo(targetFile);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }
            }
        } catch (Exception ex) {

        }

        jsonString = JsonStringConstant.getNoDataSuccessMessage("上传成功");

        commonUtill.OutputJson(jsonString, response, request);

    }

}
